<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局显示顶部公司 -->
        <tree-tools :node-data="companyInfo" :is-company="true" @showAddDialog="showAddDialog" />
        <!-- 分割线 -->
        <hr>
        <!-- 部门树形列表 -->
        <!-- default-expand-all  默认展开所有-->
        <el-tree :data="treeData" default-expand-all>
          <template v-slot="scope">
            <!-- 进行接收 -->
            <tree-tools
              :node-data="scope.data"
              :is-company="false"
              @showAddDialog="showAddDialog"
              @showEditDialog="showEditDialog"
              @reload="getDepartments"
            />
          </template>
        </el-tree>
      </el-card>
      <add-dept ref="addDept" :node-data="nodeData" :is-show-dialog="isShowDialog " @reload="getDepartments" @closeDialog="isShowDialog=false" />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { listToTree } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      companyInfo: {
        name: '传智播客黑马程序员',
        manager: '负责人',
        id: ''
      },
      treeData: [],
      isShowDialog: false,
      // 临时储存被点击的对象
      nodeData: {}
    }
  },
  created() {
    // 获取数据
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const { depts } = await getDepartments()
      this.treeData = listToTree(depts, '')
    },

    showAddDialog(nodeData) {
      console.log(nodeData)
      this.isShowDialog = true
      this.nodeData = nodeData
    },
    showEditDialog(nodeData) {
      /**
       * 显示编辑弹窗 触发数据回显
       * 点击的数据不一定完整 要获取id
       * 如何调用子组件内的函数 通过ref取得子组件中的实例 进行链式调用
       * this.nodeData = nodeData
       */
      this.$refs.addDept.getDepartDetail(nodeData.id)
      this.isShowDialog = true
      console.log(this.nodeData)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
